Change the status of a sensor through an automation

Dear,
okay?

I need your help.

I need to change the status of a sensor through an automation.

The sensor receives the data from a mqttt server

follow his code:

sensor:
  - platform: mqtt
    name: "campainha"
    state_topic: "tele/sonoffbridge/RESULT"
    value_template: '{{ value_json["RfReceived"]["Data"] }}'
    qos: 1

This is the automation that in the end should change the sensor status above for anything. You can change to “0000” or “11111”

- id: '1536891680218'
  alias: CampainhaInicio
  trigger:
  - entity_id: sensor.campainha
    platform: state
    to: '117978'
  condition: []
  action:
  - service: script.1534119754498

So personal, anyone have any idea how to change the status of the sensor through this automation?

just wondering what you’re trying to achieve. If you create an automation to change the value, as soon as your sonoff sends a new message it will be overwritten…
It may be best to create a template sensor that takes the value of your sonoff sensor and use conditions to set the value to something else?

I can not understand your reasoning …

This sensor is changed by a sonoff with tasmota … When pressing the button in the sonoff it changes the sensor to “11879”, when this happens I execute the automation …

However, it is necessary to return the sensor to any position .... so when the button is pressed and the sensor is again "11879" run the automation again.

Therefore, I need to change the sensor status through automation. I hope you have explained it better.

got it, then you don’t really want to change the value, you just want to trigger the automation every time you press the button right?
Then use the signal_received event:

  trigger:
    - platform: event
      event_type: signal_received
      event_data:
        entity_id: sensor.campainha

I did not express myself well. I need to change the value … After automation is executed … I need the sensor to return to any other information.

sensor before the automation value "xxxx!

after the automation value “YYYYY”

It is.

I understand. It’s usually a bad idea to write to a sensor which is why there is no function to write to a sensor in HA
However since this is mqtt, nothing stops you from sending a message on the sensor topic:

  - service: mqtt.publish
    data:
      topic: "tele/sonoffbridge/RESULT"
      payload: '0000'
      retain: false
1 Like

Sorry writing from my phone and can’t fornat code as yaml

I get this error

Error parsing value: 'value_json' is undefined (value: 0000, template: {{ value_json["RfReceived"]["Data"] }})

18:15 helpers/template.py (ERROR)

My automation

- id: '1536891680218'
  alias: CampainhaInicio
  trigger:
  - entity_id: sensor.campainha
    platform: state
    to: '117978'
  condition: []
  action:
  - service: script.1534119754498
  - data:
      topic: tele/sonoffbridge/RESULT
      payload: '0000'
      retain: false
    service: mqtt.publish

Yes you would have an error as we’ve basically overwritten the complete message with 0000
Try this

payload: '{"RfReceived":{"Data":"0000"}}'

1 Like

Friend! Thank you!!! it was great !!! Congratulations thank you!!!

1 Like

Hey!

thats an interesting event, never saw that before, but would certainly be able to use in several settings. where did you find info on that, I’ve checked Events - Home Assistant but no signal_received is available ? If you have a pointer, Id certainly appreciate it, thx.

Got it given here when trying to set my doorbell. I don’t have any info anywhere in afraid