Home Automation
Approximately a year ago I started to use Node-RED a little, see following post; Home Energy.
Recently I found a new use case. I am still a little old school and rather watch “Live TV” over dbv-c. I prefer it that way, because it is almost free, and I can still schedule recordings etc. In Switzerland, it is also in discussion to make ads not shippable anymore in the internet tc offerings, which is one more reason to record the shows I want to watch myself.
I am running a “TV-Headend Server”(handling my dvb-c connection) on my Home Theater PC(short HTPC) , but I don’t want to run it 24/7. That is where node-red comes in to place.
TV recordings with smart turn on/off
The requirements:
- The HTPC should not run all the time
- The HTPC should turn on only long enough to record the wanted program on live tv.
- The HTPC should only turn off, if we are not already watching something in parallel (Kodi).
- Turning it on should not interfere with current watching.
Node-RED
Turning it on
I was already using the Wake on LAN nodes to wake up my devices over the dashboard,
which is a very simple way to accomplish it. It’s also no problem if it is already on.
If one uses Node-RED as a docker container, make sure the devices are reachable by adding
this; network_mode: host
.
I used the “Inject” node to schedule it. There are more complex timers available, but I preferred the simplest, which is already in the basic Node-RED installation. One may also wonder why I wake two devices, because my HTPC has two network interfaces and, I never know which one is plugged in :D
Turning it OFF
That is the slightly more complex task, because it was not trivial to me how to detect, if someone is already watching something. I found the node ssh-v3, which let you run any command you would be able to tun over ssh. The relevant services are always started and there is no indicator on the HTPC to check if it’s just recording something or someone is watching a movie in parallel.
Then I remembered that I am monitoring the power of the whole entertainment system. The power consumption is the perfect indicator, because if only the HTPC is running, it consumes about 25 W and if the TV and the audio receiver are also running, it is somewhere between 100 and 150 W. Here cou can read up how I managed to install these power measurements.
Let me try to explain a little. I start the flow with an “Inject”. In this case I have two of
them, because on Tuesdays I want to record something additional. These first nodes inject a
variable in msg.command
, which is already the shutdown now
instruction for the ssh connection
later. In the next Influx DB node I get the latest value of the power measurement with:
SELECT last("value") FROM "power_sens2"
Then in the next connected function node I put the logic like that:
if (msg.payload[0].last > 50){
msg.payload = 'uptime';
}else{
msg.payload = msg.command;
}
return msg;
Which should speak for itself. I replace the shutdown command by something innocent(uptime
),
but there may be better solutions which just interrupt the flow there.
Finally, it goes to the ssh node. And for debug reasons I added two debug nodes (one for the payload and one for the session of the ssh connection).
TV-Headend / Kodi
Map recordings to NAS
My HTPC has limited resources, so I wondered if I can somehow mount a nfs-share from my NAS. I am using LibreELEC and their documentation provides good instructions, see here.
The only thing I changed was to set Before=kodi.service
to Before=service.tvheadend42.service
,
because in my case it turned out to be more stable, otherwise the recording were only partially
loaded. Following my entire service file:
[Unit]
Description=nfs share for tv recordings
Requires=network-online.service
After=network-online.service
Before=service.tvheadend42.service
[Mount]
What=10.32.1.5:/mnt/tralala/kodi_recordings
Where=/storage/recordings
Options=
Type=nfs
[Install]
WantedBy=multi-user.target
Schedule the recording in TV-Headend
I simply created a timer in TV-Headend, which can be done from the Kodi interface or over the web frontend f TV-Headend. It might be easier to use the web frontend..
Like that I covered all my requirements and enjoy the setup ever since :D