A Smarter Smart TV

My TV at home features both Google TV through a Xiaomi Mi Box, and Kodi running on an old Chromebox. This alone is what most would call, a ‘smart tv’

However Is it really smart if you still have to turn it on? and change channel?
In some occasions, HDMI CEC can help here, as it is capable of turning the TV on and changing to the input it needs ! However for my use case, this doesnt work. and it is rarely able to turn OFF the TV.

Luckily, in one of my recent posts I covered how I added RS232 control to the TV. so actually changing inputs and power on and off are now discrete commands we have at our disposal!

Using some simple logic in Node-Red it was a simple case of checking which device changes to playing, and then switching the TV on, and changing over the input to match!

There is probably a more streamlined way to do this but this is what I have come up with:

It works like so;

First we have this state changed node that outputs true, if the device is not playing.

If it IS playing, it outputs a false, which triggers the ‘Turn on TV’ call service node.

followed by a wait until node, just to add a 1 second delay to allow the TV to startup.

and finally another call service node to actually change the input!

Meanwhile, if the TV state is anything OTHER than playing, it will first go to this wait until node, and wait 60 minutes in case it begins playing again (this gives time to choose the next video or show using a remote ! I will probably shorten this though)

finally followed by the turn off switch for the TV.

I also need to get around to renaming the entities of the switches used here, as they’re not very well named at present.

Ill include the flow below!

[{"id":"4e63a44b.f401bc","type":"server-state-changed","z":"e94d3e8b.2d81e","name":"When Kodi stops Playing","server":"33a2704d.0e654","version":1,"entityidfilter":"media_player.kodi_libreelec_local","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"playing","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":290,"y":760,"wires":[["9a3def91.d13ae"],["432da1ba.4b195"]]},{"id":"432da1ba.4b195","type":"api-call-service","z":"e94d3e8b.2d81e","name":"Turn on TV","server":"33a2704d.0e654","version":"1","service_domain":"homeassistant","service":"turn_on","entityId":"switch.sharp_tv_rs232","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":850,"y":700,"wires":[["9be17d6c.92cdf"]]},{"id":"11a5db01.9c8e75","type":"api-call-service","z":"e94d3e8b.2d81e","name":"Turn off TV","server":"33a2704d.0e654","version":1,"service_domain":"homeassistant","service":"turn_on","entityId":"switch.turn_off","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":850,"y":780,"wires":[[]]},{"id":"9a3def91.d13ae","type":"ha-wait-until","z":"e94d3e8b.2d81e","name":"","server":"33a2704d.0e654","outputs":2,"entityId":"media_player.kodi_libreelec_local","property":"state","comparator":"is","value":"playing","valueType":"str","timeout":"60","timeoutUnits":"minutes","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":620,"y":760,"wires":[["432da1ba.4b195"],["11a5db01.9c8e75"]]},{"id":"9f2ad7cf.baa538","type":"api-call-service","z":"e94d3e8b.2d81e","name":"Change Input","server":"33a2704d.0e654","version":1,"service_domain":"switch","service":"turn_on","entityId":"switch.displayport","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1200,"y":680,"wires":[[]]},{"id":"9be17d6c.92cdf","type":"ha-wait-until","z":"e94d3e8b.2d81e","name":"","server":"33a2704d.0e654","outputs":2,"entityId":"switch.displayport_2","property":"state","comparator":"is","value":"on","valueType":"str","timeout":"1","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1000,"y":680,"wires":[["9f2ad7cf.baa538"],["9f2ad7cf.baa538"]]},{"id":"33a2704d.0e654","type":"server","z":"","name":"Home Assistant [Lewys]","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Upgrading from ResinOS to Ubuntu

My Home Assistant / Hass.io install was an old ResinOS image from the hass.io website running on an Intel NUC.

Occasionally I would have to reboot the machine, as it would loose network connectivity for an unknown reason (The machine was still connected, and the host OS, but Home Assistant could not connect out to the internet? as in, the logs were full of time outs and refused connections due to ‘max retries exceeded’

I wanted to migrate this over to a Ubuntu Docker install so that I had better control over the hardware and could do other tasks on the machine, and to hopefully repair the issue with connectivity.

I took a snapshot of hass.io through the web UI, copied it off as well as manually backed up the files, and then installed Ubuntu 18.04.3 onto the NUC.

I installed Hass.io following the manual install directions on the website, and then the samba addon. Copied the file back into the correct folder and rebooted.
Once it was back up, I selected the snapshot and hit restore.

It took about half an hour to complete for some reason, but once it had, it was pretty much exactly how it was before I started.

There was a few things that required attention, for example Node-Red was not connecting. I didn’t do anything to it but restarting and it connected once more.
There was also problems with MariaDB that I couldn’t understand, it could not connect, but was listening and running fine? I ended up just removing this addon and reinstalling it, as it was pretty simple.

Once it was all up and running again, It seemed to be working fine. I setup Portainer manually and connected it to the local docker instance, and added my other docker endpoints to it.

All was good.

Until the next day. My alarm didnt go off in the morning, which was the first red flag, I tried opening Home Assistant to adjust my Air Conditioner and it would not load, I checked Portainer and the container was still running, so it should be working? Checking the logs it looks like its running but cannot connect to the internet again. This is similar but not exactly the same messages as previously. I restarted the Home Assistant container and the logs indicated it was having connectivity problems out to the internet, but this should not stop the Local UI from working on https://IP:8123 … yet it was? I had to leave for work at this point so I figured no biggie, ill fix it on lunch over VPN.

(update: the alarm didn’t go off because the MP3 it plays is hosted on the internet, I need to switch it over to a local MP3 to remove this internet dependency)

But heres where the plot thickens.

Just yesterday I was toying with settings on my OpenVPN Server with my buddy Tom while we were trying to figure out why DNS doesnt work for local clients when connected to VPN, and something we touched must have been wrong as now my VPN is playing up and apparently he is having internet troubles too.

The next day:

So it turns out I didn’t break anything, Telstra had messed up my account for some reason and after an hour talking to them they were able to revert their changes and fix it. Now everything’s back to normal!

Updating my last Arduino based ESP to ESPHome

I have most of my ESP based IoT devices running ESPHome by now, but there was one left that I hadn’t spent the time figuring out how to adapt.

That was the one that lives inside my wall, and opens the buildings door by shorting two contacts on the intercom system.

My requirements for this were backwards compatibility so that my scripts and automations that were setup to control the door via MQTT could still function, this way I can continue to operate as normal but with the addition of OTA updates and telemetry from the node thats hardest to reach!

The original code involved subscribing to a topic, and waiting for a payload, then turning the relay on and then off again, for a momentary press.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define CLIENT_ID "buildingdoor-singlerelay"

#define RELAY_PIN 0


// Update these with values suitable for your network.
const char* ssid = "WiFiSSID";
const char* password = "WiFiPassword";
const char* mqtt_server = "MQTTServerIP";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);
  setup_wifi(); 
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  digitalWrite(RELAY_PIN, HIGH);
  pinMode(D3, OUTPUT);
  pinMode(D1, OUTPUT);
  digitalWrite(D1, LOW);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid); //We don't want the ESP to act as an AP
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    client.publish("building/door/relay", "1");
    delay(100);
    digitalWrite(RELAY_PIN, LOW);
    delay(200);
    digitalWrite(RELAY_PIN, HIGH);
  }  } else {
    digitalWrite(RELAY_PIN, HIGH);
    delay(100);
    client.publish("building/door/relay", "0");
  }

}

void reconnect() {
  // Loop until we're reconnected
  digitalWrite(LED_BUILTIN, LOW);
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(CLIENT_ID)) {
      Serial.println("connected");
      client.subscribe("building/door/relay/set");
      digitalWrite(LED_BUILTIN, HIGH);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

The new ESPHome code, does this, in addition to supporting the Home Assistant API and reporting back some values such as the WiFi Signal Strength!

esphome:
  name: buildingdoor
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: 'WiFiSSID'
  password: 'WiFiPassword'
  domain: .local
  fast_connect: true
  manual_ip:
    static_ip: 172.16.0.XX
    gateway: 172.16.0.1
    subnet: 255.255.255.0
mqtt:
  broker: 172.16.0.XX
  username: MQTTUsername
#  password: MyMQTTPassword
  on_message:
    topic: building/door/relay/set
    then:
      - switch.turn_on: building_door_switch
api:

# Enable logging
logger:

ota:

switch:
  - platform: gpio
    pin:
      number: 0
      inverted: yes
    icon: "mdi:office-building"
    name: "Building Door Open Switch"
    id: building_door_switch
    retain: false
    discovery: false
    availability:
      topic: building/door/status
      payload_available: online
      payload_not_available: offline
    state_topic: building/door/relay
    command_topic: building/door/relay/set
    on_turn_on:
    - logger.log: "Building Door Relay Activated!"
    - delay: 0.2s
    - switch.turn_off: building_door_switch
    on_turn_off:
    - logger.log: "Building Door Relay Deactivated!"

sensor:
  - platform: wifi_signal
    name: "Building Door WiFi Signal"
    update_interval: 60s
text_sensor:
  - platform: wifi_info
    ip_address:
      name: Building Door ESP IP Address
    ssid:
      name: Building Door ESP Connected SSID
    bssid:
      name: Building Door ESP Connected BSSID

I first connected it externally next to the wall, and listened to hear that they indeed, both fired when they were supposed to, and once confirmed, I opened up the wall, switched out the ESP-01S modules and closed it all back up!