Extracting the numeric value from a payload data

Unfortunately I have no programming and templating skills

I have a few thermostatic valves (identified according to their MAC address) that cyclically publish a json to a MQTT topic.

{"trv":"00:1A:22:16:DA:9B","temp":"23.5","offsetTemp":"0.0","valve":"76% open","mode":"manual","boost":"inactive","window":"closed","state":"unlocked","battery":"GOOD"}

I need to publish just the numeric value of “valve” data from the above json to a different topic.

Ie in the case above:

"valve":"76% open"

I need to publish the number 76 on a different topic

The code that I prepared simply publishes the entire “valve” data without any operation.

- alias: eq3mqtt_valves_open_status
  description: ''
  trigger:
  - platform: mqtt
    topic: /eq3mqttradout/status
  condition: []
  action:
    service: mqtt.publish
    data_template:
      topic: "/eq3mqtt/{{ trigger.payload_json.trv }}/openstatus"
      payload: "{{ trigger.payload_json.valve }}"

Any help in finding a way to publish just the number would be appreciated.

Thanks

payload: "{{ trigger.payload_json.valve | regex_replace('[^\\d]','') | int }}"

thanks for the prompt help
I get the following error when validating the configuration :

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations.yaml", line 661, column 7
expected <block end>, but found '['
  in "/config/automations.yaml", line 662, column 64

My bad.

  • use regex_replace('[^\\d]','') (single quotes instead of double quotes)
  • also, you missed the underscore between regex and replace

Awesome!
It now works like a charm!
Thank You!

(the underscore is there. It is just my stupid code editor that doesn’t show it)

1 Like