Correct status of relay script

Hello all,

I’ve got a RF433Mhz device - EV1527 which is a relay that turns on/off lamps in my house (it’s showed in HA as light.rfcustom_524643_0 in my case).
I also have a PZEM004 electricity monitor between the relay and lamp sockets. If I start switching on and off EV1527 the status of the switched lamp may be wrong (the status in HA will be showed wrong - physical light will be ON and the status in HA will be OFF).
How can I set the right status of the relay depending of value from PZEM004 electricity monitor (sensor.pobor_pradu_swiatlo_korytarz_energy_current) - for example if I switch on light the electricity monitor will show that current is 0.2 A [higher than 0], in that case change status of light switch to ON and if current is 0 A then change status to off).

Thank you in advance for any tip!

Simply run an automations so that current value below or above threshold => turn on or off the rf433 device. Doesn’t matter if it was already turned on/off manually.

Tried to implement this, but still it doesn’t show the correct state of the relay. What am I doing wrong?

- alias: "Żarówka Wyłączona jeżeli stan z licznika 0A"
    platform: state
    entity_id: sensor.pobor_pradu_swiatlo_korytarz_energy_current
    - condition: template
      value_template: '{{ states.sensor.pobor_pradu_swiatlo_korytarz_energy_current.state < 0.1 and states(light.rfcustom_524643_0) == "on" }}'
    service: light.turn_off
      entity_id: light.rfcustom_524643_0

Use this as trigger and lose the condition.

You need two automations. One for turn on and another one for turn off.

Thank You for help - I wrote two automations:

- alias: "Żarówka Wyłączona jeżeli stan z licznika 0A"
    - platform: numeric_state
      entity_id: sensor.pobor_pradu_swiatlo_korytarz_energy_power
      below: 5
      for: '00:00:30'
    - service: light.turn_off
      entity_id: light.rfcustom_524643_0


- alias: "Żarówka Włączona jeżeli stan z licznika powyżej 0A"
    - platform: numeric_state
      entity_id: sensor.pobor_pradu_swiatlo_korytarz_energy_power
      above: 20
      for: '00:00:30'
    - service: light.turn_on
      entity_id: light.rfcustom_524643_0

Indeed it changes the state of light depending on energy meter but I think there’s another problem - the relay just toggle its status - doesn’t matter if I send light.on or command - with those 2 automations it will keep switch on and off - I wonder if I can bypass it somehow?

I’m not familiar with the hardware you use but is there different rf packets for on and off? Do you have separate remote for it?

Another idea:
make a template sensor like this

It will show the state as sensor and then make a script running e.g. the turn_on command. use that as toggle button.

Create a template light :

   - platform: template
         friendly_name: "light by sensor"
         value_template: >-
            {% if states.sensor.pobor_pradu_swiatlo_korytarz_energy_current.state < 0.1 %}
            {% else %}
            {% endif %}       
           service: light.turn_on
           entity_id:  light.rfcustom_524643_0
             entity_id:  light.rfcustom_524643_0
           service: light.turn_off
           entity_id:  light.rfcustom_524643_0
             entity_id: light.rfcustom_524643_0

Edit : switched on/off in the value_template

Thank You for both replies - when I used the code above the result is that the template light turns on and off the relay but the status of "light by sensor’ entity changes for ON and then it goes OFF each time - I’m not sure if it ‘reads’ the correct value from energy monitor (PZEM-004 has a delay for like 10 sec. after its update itself).

In logs I found:

Update for light.light_by_sensor fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/", line 281, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/", line 459, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/", line 272, in async_update
    state = self._template.async_render().lower()
  File "/usr/src/homeassistant/homeassistant/helpers/", line 222, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: '<' not supported between instances of 'str' and 'float'