Question: how can I change the state_topic
of a sensor on a button press?
Background: I have multiple ESP8266 boards that publish data to a central RPi running InfluxDB over MQTT. To distinguish the boards I encode location info in the state topic, e.g.
state_topic: influx/environv3/location/mobile/value/state
where the topis parsed by a MQTT listener script as follows:
influx/<measurement>/[<tagname>/<tagvalue>/]*<field>/state
The problem I have: one board I use in multiple locations (fine dust measurement), so I want to change the state topic on the fly. I have a button on this board which I hope to use to change the state_topic
. In pseudo-code, I’m looking for something like:
globals:
- id: location_state
type: int
restore_value: no
initial_value: '0'
sensor:
- platform: sds011
pm_2_5:
state_topic: influx/environv3/location/home/value/state
id: mypm25
binary_sensor:
- platform: gpio
on_press:
then:
- lambda: |-
if (id(initial_value) == 0) {
id(mypm25).state_topic = "influx/environv3/location/mobile/value/state"
id(initial_value) += 1;
} if (id(initial_value) == 1) {
id(mypm25).state_topic = "influx/environv3/location/office/value/state"
id(initial_value) += 1;
} else {
id(mypm25).state_topic = "influx/environv3/location/home/value/state"
id(initial_value) = 0;
}
Unfortunately, .state_topic
is not a member of the sensor that I can change. Does anyone have any ideas?