MQTT Fan configuration state/speed template for RF Ceiling Fan

Hi,

I have 3 315MHz RF Ceiling fans in my house. I have setup a receiver and transmitter with OpenMQTTGateway which is working wonderfully after failing to get ESPHome working.

I am attempting to setup the fans in my configuration based on the codes I sniffed from the remotes. The remote has 4 buttons: Off, Speed 1, Speed 2, Speed 3, and Speed 4. Each button sends a different code. The payload looks as follows:

Message 0 received on home/OpenMQTTGateway/433toMQTT at 6:24 PM:

{
    "value": 522775,
    "protocol": 6,
    "length": 24,
    "delay": 482
}

I tried to setup the fan using the template examples I’ve found and have fooled around with getting the fan state/speed state to stay in sync with the remote/HA. I think I am just misunderstanding how the templates should be used. I have posted the config that I am trying to use and hope someone can set me straight. Thank you.

fan:
- platform: mqtt
  name: office_ceiling_fan
  unique_id: fan.office_ceiling_fan
  state_topic: "home/OpenMQTTGateway/433toMQTT"
  state_value_template: >-
    {% if value_json.value == 522887 %}
       {{'ON'}}
    {% elif value_json.value == 522823 %}
       {{'ON'}}
    {% elif value_json.value == 522951 %}
       {{'ON'}}
    {% elif value_json.value == 522775 %}
       {{'OFF'}}
    {% else %}
       {{ states('fan.office_ceiling_fan') | upper }}
    {% endif %}
  speed_state_topic: "home/OpenMQTTGateway/433toMQTT"
  speed_command_topic: "home/OpenMQTTGateway/commands/MQTTto433"
  speed_value_template: >-
    {% if value_json.value == 522887 %}
       {{'low'}}
    {% elif value_json.value == 522823 %}
       {{'medium'}}
    {% elif value_json.value == 522951 %}
       {{'high'}}
    {% elif value_json.value == 522775 %}
       {{'off'}}
    {% else %}
       {{ state_attr('fan.office_ceiling_fan', 'speed') | upper }}
    {% endif %}
  command_topic: "home/OpenMQTTGateway/commands/MQTTto433"
  payload_off: '{"value":522775,"protocol":6,"length":24,"delay":481}'
  payload_low_speed: '{"value":522887,"protocol":6,"length":24,"delay":481}'
  payload_medium_speed: '{"value":522823,"protocol":6,"length":24,"delay":481}'
  payload_high_speed: '{"value":522951,"protocol":6,"length":24,"delay":481}'
  speeds:
    - 'off'
    - low
    - medium
    - high
  availability_topic: "home/OpenMQTTGateway/LWT"
  payload_available: "online"
  payload_not_available: "offline"
  qos: 0

Kinda been limping through with a 1/2 working system. Anyone have insight on this?