Dim lights when watching TV

Hi,

I have created a Node-RED flow to dim the lights when watching television. Currently, this flow is also executed when the television is off. This is not OK. The purpose of the flow is that it is enabled only when the television is on AND the lights are on. Can you please help me to solve this issue?


The flow consists of the following parts:

  • The ping node pings the fixed IP address of the television. When the TV is off, the result is “false”. When the TV is on, it displays the trip time in ms.
  • To convert the results above to true or false, I have added a function node which contains the following code:
if (typeof msg.payload === 'boolean')
 { msg.payload = false; }
else
 { msg.payload = true; }
return msg;
  • Next step is to use a Current State node to check the status of the lighting. If lighting is on, then the flow continues.
  • The last step consists of a “Call Service” node, which makes sure that the lighting will be dimmed. The service “turn_on” is used and to make sure that the lights are dimmed, the following code has been added to the data field:
{"entity_id":"light.fibaro_fgd212_verlichting_woonkamer","brightness_pct":25}

Thanks in advance for your support!

If you don’t want to always run the flow, then you have to have something to detect when the TV is on. In my case, my TV is turned on using a Shelly. So, I know what to do when I have this event. Is there a way for you to detect when the tv on outside this flow?
GV

Hi, using the ping node and the function node, I am able to check if the TV is on or off.

Check yes, but in order to do that, you need to run your flow every x seconds or minutes.
If you don’t want that you need somehow to get an event, “tv turned on”.
That’s the difference between polling (what you are doing here) and receiving an event.
GV

ok, that is clear. Do you know how to create such an event in NodeRED?

Edit;
Try this:

[{"id":"273bb827.9c15b8","type":"switch","z":"60dee41c.0d999c","name":"False or True?","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"istype","v":"number","vt":"number"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":300,"wires":[[],[]]}]

Well, the problem is not to create the event in node-red but to have an event you can rely on.
When you turn on your tv, is there a way to be informed of it (in opposition of polling)?
It could be a sensor to detect the power consumption.
Another option is to turn on your TV using a shelly and then you know when it is on…
The “problem” is therefore before nodered. Then, depending on what you are able to get as an event, you will have to translate that into NR.
GV

Hi, I have changed the Ping node to a Events State node, which is checking if the Unifi device tracker for the television is “Thuis” or “Afwezig”. When the state of this entity is “Thuis”, it means that the television has an active network connection.
However, the flow is not working anymore. Do you know what needs to be adapted? Do I need to add a switch?

A simple way to do this is to create a ping sensor in HA and use that in a events-state node in NodeRed. When true, the TV is on (ping replies), when false, the TV is off.

Does the device tracker return “thuis” or “home”? (I’ve not dealt with HA and other languages).

I have checked the status of the Entity and have seen that the state is “home” instead of “thuis”. The entity shows “Thuis” in Lovelace, but “home” in the Entity section of Developer tools. I have changed the Events state node and now it is working! Thanks!

1 Like

Hi greengolfer, thanks for your input. Based on the Unifi device tracker, the Ping node is not required anymore and the flow will not start every 20 seconds anymore.